import java.io.*;

/**
 * 分别测试
 * 字节流,一个一个的复制,一串一串的复制
 * 缓冲流一个一个的复制,一串一串的复制的速度
 */
public class ByteBufferTime {
    //定义好公用的资源
    private static final String SRC_FILE = "C:\\panGod\\守F公民.[中英双字.1024分辨率].rmvb";
    private static final String DEST_FILE = "C:\\data\\test\\";
    public static void main(String[] args) {
//        copy1();字节流一个一个复制,这个速度太慢,直接淘汰
        copy2();//字节流1k1k的复制,速度还可以
//        copy3();缓冲流一个一个字节复制,这个也很慢
        copy4();//缓冲流1k1k的复制,速度很快
    }
    public static void copy1(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "test1.rmvb");
                ) {
            int a;
            while((a = is.read())!=-1){
                os.write(a);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("字节流一个一个字节的复制速度为:" + (endTime-startTime)/1000 + "s.");
    }
    public static void copy2(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "test2.rmvb");
        ) {
            int len;
            byte[] buffer = new byte[1024];
            while((len = is.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("字节流1K1K字节的复制速度为:" + (endTime-startTime)/1000 + "s.");
    }
    public static void copy3(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "test3.rmvb");
                InputStream bis = new BufferedInputStream(is);
                OutputStream bos = new BufferedOutputStream(os);
        ) {
            int a;
            while((a = bis.read())!=-1){
                bos.write(a);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("缓冲流一个一个字节的复制速度为:" + (endTime-startTime)/1000 + "s.");
    }
    public static void copy4(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE + "test4.rmvb");
                InputStream bis = new BufferedInputStream(is);
                OutputStream bos = new BufferedOutputStream(os);
        ) {
            int len;
            byte[] buffer = new byte[1024];
            while((len = bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("缓冲流1K1K字节的复制速度为:" + (endTime-startTime)/1000 + "s.");
    }
}
